// ActionScript file g
package AS3 {
import AS3.constants.FossilStateNames;

import AS3.constants.combo.EnergyProduction;
import AS3.constants.combo.IndustrialEmissionReduction;

import AS3.constants.combo.TransportEmissionReduction;

import flash.geom.Point;

import mx.containers.Canvas;
import mx.controls.Alert;
import mx.controls.Label;
import mx.controls.TextArea;
import mx.core.FlexGlobals;
import mx.formatters.NumberBaseRoundType;
import mx.formatters.NumberFormatter;
import mx.managers.ToolTipManager;

import flash.events.TimerEvent;
import flash.utils.Timer;


import zakladki.AS3.ConsumptionSelectedData;
import zakladki.AS3.EmissionSelectedData;
import zakladki.AS3.FoodSelectedData;
import zakladki.AS3.HouseSelectedData;
import zakladki.AS3.PublicTransportSelectedData;
import zakladki.AS3.VehicleSelectedData;
import zakladki.AS3.WarmWaterSelectedData;
import zakladki.WarmWaterPanel;

import zakladki.TopPanel;
import zakladki.MenuControlsPanel;
import zakladki.HousePanel;
import zakladki.ToolTipPanel;
import zakladki.PrivateTransportPanel;
import zakladki.PublicTransportPanel;
import zakladki.ViewContentPanel;
import zakladki.ChoseCountryPanel;
import zakladki.PlotPanel;
import zakladki.FoodPanel;
import zakladki.InfoPanel;


public class MainProcessor {

    //custom AS3 classes
    private var mainCalculator:MainCalculator;
    private var controlButtonStates:ButtonStatesController;
    private var coefficientCalculator:CoefficientCalculator;
    private var countryPostInitializer:CountryPostInitializer = new CountryPostInitializer();


    private var person:Person = new Person("");
    public var chosenCountry:Country;

    private var oldEmissions:CountryEmission;
    private var newEmissions:CountryEmission;

    //values for plot
    private var oldConverted:ConvertedEmission;
    private var newConverted:ConvertedEmission;

    //AS3 classes
    public var persons:Array = new Array();
    public var activePerson:int;
    public var minuteTimer:Timer = new Timer(500, 1);

    // custom mxml components
    private var mainPlot:PlotPanel;
    private var countrySelector:ChoseCountryPanel;
    private var newToolTip:ToolTipPanel;
    private var contentView:ViewContentPanel;
    private var topPart:TopPanel;
    private var mainMenu:MenuControlsPanel;
    private var infoControls:InfoPanel;

    // sdk mxml components
    private var countryXML:XML;   //setter
    private var canvas1:Canvas;  //setter
    private var aboveAverageCO2Label:Label;  // setter
    private var emissionTexts:XML;  //setter
    private var textArea:TextArea;  //setter
    private var textArea1:TextArea; //setter
    private var textArea2:TextArea;  //setter

    private var mainApp:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;

    private var units:int;
    private var firstInitialization:Boolean = true;

    public function MainProcessor() {

    }

    public function getCurrentPerson():Person {
        return persons[activePerson];
    }

    public function getUserScore():int {
        var person:Person = persons[activePerson];
        return person.score;
    }

    public function updateUserScore(score:int,correctAnswerCnt:int,wrongAnswerCnt:int):void {
        var person:Person = persons[activePerson];
        person.score = person.score + score;
        person.correctAnswerCnt = person.correctAnswerCnt + correctAnswerCnt;
        person.wrongAnswerCnt = person.wrongAnswerCnt + wrongAnswerCnt;
    }

    public function initializeFossilminator():void {
        persons.push(person);
        ToolTipManager.enabled = false;
    }

    public function start(country:Country, countryEmissions:CountryEmission, unit:int):void {
        this.chosenCountry = country;
        this.oldEmissions = countryEmissions;
        this.newEmissions = null;
        this.units = unit;
        chosenCountry = countryPostInitializer.postInit(chosenCountry);

        topPart.initializedTopPanel(chosenCountry.namePl);

        if (!firstInitialization) {
            mainMenu.houseClicked();
            controlButtonStates.setControls(chosenCountry, persons, true);
            controlButtonStates.SetParametersMinMax(2, persons);
            initPersons(persons);
            controlButtonStates.setVisFromParameters(persons[activePerson]);
            controlButtonStates.setParametersFromVis(persons[activePerson]);
            calculateClick(unit);
        }
        else {
            changeViewState(10);
        }
        firstInitialization = false;
    }

    public function startMenuControls():void {
        mainMenu.houseClicked();

        controlButtonStates.setControls(chosenCountry, persons, true);
        controlButtonStates.SetParametersMinMax(2, persons);
        initPersons(persons);
        controlButtonStates.setVisFromParameters(persons[activePerson]);
        controlButtonStates.setParametersFromVis(persons[activePerson]);
        calculateClick(units);
    }

    public function initPersons(osoby:Array):void {
        for (var i:int = 0; i < osoby.length; i++) {
            var person:Person = osoby[i];
            person.setVehicleData(0, new VehicleSelectedData(0, 0, 0, 0, 0, 0, 0));
            person.setVehicleData(1, new VehicleSelectedData(0, 0, 0, 0, 0, 0, 0));
            person.setVehicleData(2, new VehicleSelectedData(0, 0, 0, 0, 0, 0, 0));

            person.longDistanceBus = 0;
            person.suburbanTrain = 0;
            person.longDistanceFlights = 0;
            person.shortDistanceFlights = 0;

            person.cityBus = 0; // CityBus
            person.longDistanceBus = 0; // Bus
            person.miniBus = 0; // Mikrobus
            person.tramSubway = 0; // Tram/metro
            person.suburbanTrain = 0; // Pociąg podmiejski
            person.longDistanceTrain = 0; // Pociąg dalekobieżny
            person.taxi = 0; // Taxi
        }
    }

    public function toolTipStart(method:String):void {
        minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
        minuteTimer.start();
        newToolTip.setHelp(method);
    }

    private function toolTipGlobe():void {
        newToolTip.lewy_dol.visible = false;
        newToolTip.lewy_gora.visible = false;
        newToolTip.prawy_dol.visible = false;
        newToolTip.prawy_gora.visible = false;
    }

    public function resetBigTip():void {
        minuteTimer.reset();
        closeBigTip();
        minuteTimer.start();
    }

    public function destroyBigTip():void {
        minuteTimer.reset();
        closeBigTip();
    }

    public function closeBigTip():void {
        newToolTip.visible = false;
        minuteTimer.reset();
    }

    public function onTimerComplete(event:TimerEvent):void {
        var pt:Point = new Point(FlexGlobals.topLevelApplication.mouseX, FlexGlobals.topLevelApplication.mouseY);

        if (canvas1.mouseX < 899 && canvas1.mouseY < 696) {
            newToolTip.x = canvas1.mouseX + 20;
            newToolTip.y = canvas1.mouseY - 20; //toolTipGlobe();newtoolTip.lewy_gora.visible = true;}
        }
        else if (canvas1.mouseX < 899 && canvas1.mouseY > 696) {
            newToolTip.x = canvas1.mouseX - 20;
            newToolTip.y = canvas1.mouseY - 299; //toolTipGlobe();newtoolTip.lewy_dol.visible = true;}
        }
        else if (canvas1.mouseX > 899 && canvas1.mouseY < 696) {
            newToolTip.x = canvas1.mouseX - 547;
            newToolTip.y = canvas1.mouseY + 299; //toolTipGlobe();newtoolTip.prawy_gora.visible = true;}
        }
        else {
            newToolTip.x = canvas1.mouseX - 547;
            newToolTip.y = canvas1.mouseY - 299; //toolTipGlobe();newtoolTip.prawy_dol.visible = true;}
        }
        newToolTip.visible = true;
    }


//</Obsługa toolTIp>

    public function changeFossilUnits(unit:int):void {
        var optimize:Boolean = newEmissions != null;
        var oldEmissionConverted:ConvertedEmission = EmissionHelper.convertToUnits(oldEmissions, unit);
        var newEmissionConverted:ConvertedEmission = newEmissions != null ? EmissionHelper.convertToUnits(newEmissions, unit) : null;
        mainPlot.plotEmissions(oldEmissionConverted, newEmissionConverted, chosenCountry, optimize);

    }

    public function calculateClick(units:int):void {
        try {
            doCalculation(units);
        } catch(e:Error) {
            Alert.show("doCalculation error: " + e.message);
        }

        try {
            setPlotValues(false);
        } catch(e:Error) {
            Alert.show("setPlotValues error: " + e.message);
        }
    }

    private function doCalculation(units:int):void {
        if (newEmissions != null) {
            newEmissions = mainCalculator.calculateClick(chosenCountry, newEmissions, persons[activePerson])
        }
        else {
            oldEmissions = mainCalculator.calculateClick(chosenCountry, oldEmissions, persons[activePerson]);
        }

        oldConverted = EmissionHelper.convertToUnits(oldEmissions, units);
        newConverted = newEmissions != null ? EmissionHelper.convertToUnits(newEmissions, units) : null;
    }

    private function setPlotValues(showSummary:Boolean):void {
        var optimize:Boolean = newConverted != null;

        mainPlot.plotEmissions(oldConverted, newConverted, chosenCountry, optimize);

        setInfoTexts();

        if (showSummary) {
            initializeSummary();
            mainPlot.displayNew();
        }
    }

    public function houseUpdate(selectedData:HouseSelectedData):void {
        var person:Person = persons[activePerson];

        person.houseType = selectedData.houseType;
        person.buildingMaterial = selectedData.buildingMaterial;
        person.insulation = selectedData.insulation;
        person.livingArea = selectedData.area;
        person.personNumber = selectedData.personNumber;
        person.heatingTemperature = selectedData.heating;
        person.heatingSource = selectedData.heatingSource;
        person.ventilationMethod = selectedData.ventilationType;
        person.heatingRenewableRatio = selectedData.renewableRatio;
    }

    public function warmWaterUpdate(selectedData:WarmWaterSelectedData):void {
        var person:Person = persons[activePerson];

        person.bathNumber = selectedData.bathNumber;
        person.waterBathVolume = selectedData.bathVolume;
        person.showerNumber = selectedData.showerNumber;
        person.waterHeatingSystem = selectedData.waterHeatingSource;
        person.acEnabled = selectedData.acEnabled;
        person.acFrequency = selectedData.acFrequency;
        person.acDuration = selectedData.acDuration;
        person.acTemp = selectedData.acTemp;
    }

    public function privateTransportUpdate(number:int, vehicleData:VehicleSelectedData):void {
        var person:Person = persons[activePerson];
        person.setVehicleData(number, vehicleData);
    }

    public function publicTransportUpdate(selectedData:PublicTransportSelectedData):void {
        var person:Person = persons[activePerson];

        person.cityBus = selectedData.cityBus;
        person.longDistanceBus = selectedData.longDistanceBus;
        person.miniBus = selectedData.miniBus;
        person.tramSubway = selectedData.tramSubway;
        person.suburbanTrain = selectedData.suburbanTrain;
        person.longDistanceTrain = selectedData.longDistanceTrain;
        person.taxi = selectedData.taxi;
        person.flightClass = selectedData.flightClass;
        person.longDistanceFlights = selectedData.longDistanceFlights;
        person.shortDistanceFlights = selectedData.shortDistanceFlights;
    }


    public function foodUpdate(foodData:FoodSelectedData):void {
        var person:Person = persons[activePerson];

        person.foodAmount = foodData.foodAmount;
        person.foodOrigin = foodData.foodOrigin;
        person.meatConsumption = foodData.meatConsumption;
        person.frozenFood = foodData.frozenFood;
        /*************        KUCHENKA!!!!!!    *************/
        person.fridgeType = foodData.fridgeType;
        person.separateFridge = foodData.separateFridge;

        person.washingMachineSelected = foodData.washingMachineSelected;
        person.cookerType = foodData.cookerType;
    }

    public function consumptionUpdate(selectedData:ConsumptionSelectedData):void {
        var person:Person = persons[activePerson];

        person.income = selectedData.income;
        person.recycling = selectedData.recycling; // Some of my waste is recycled
        person.clothes = selectedData.clothes; // New, >5y
        person.packaging = selectedData.packaging; // I tend to buy things that are nicely packed
        person.furnitureElectronics = selectedData.furnitureElectronics; // I mostly buy  second hand furniture and appliances
        person.recreation = selectedData.recreation; // I prefer medium carbon intensive time spending, e.g. movies or bars
        person.electricity = selectedData.electricity;
        person.renewableRatio = selectedData.renewableRatio;

        person.lighting = selectedData.electricLightningSelected;

        person.underwearDryer = selectedData.underwearDryerSelected;
        person.tv = selectedData.tvSelected;
        person.videoDVD = selectedData.videoDVDSelected;
        person.cableTV = selectedData.cableTVSelected;
        person.computer = selectedData.computeSelected;
        person.radioHiFi = selectedData.radioHiFiSelected;
    }

    public function emissionUpdate(emissionData:EmissionSelectedData):void {
        var person:Person = persons[activePerson];

        try {
            person.industrialEmissionReduction = IndustrialEmissionReduction.getReduction(emissionData.industrialEmissionReduction);
            person.transportEmissionReduction = TransportEmissionReduction.getReduction(emissionData.transportEmissionReduction);

            if (emissionData.energyProduction == EnergyProduction.ACTUAL) {
                chosenCountry.resetOriginalValues();
            }
            else if (emissionData.energyProduction == EnergyProduction.COAL) {
                chosenCountry.coalEnergy = 1;
                chosenCountry.hydroEnergy = 0;
                chosenCountry.oilEnergy = 0;
                chosenCountry.gasEnergy = 0;
                chosenCountry.nuclearEnergy = 0;
            }
            else if (emissionData.energyProduction == EnergyProduction.OIL) {
                chosenCountry.coalEnergy = 0;
                chosenCountry.hydroEnergy = 0;
                chosenCountry.oilEnergy = 1;
                chosenCountry.gasEnergy = 0;
                chosenCountry.nuclearEnergy = 0;
            }
            else if (emissionData.energyProduction == EnergyProduction.GAS) {
                chosenCountry.coalEnergy = 0;
                chosenCountry.hydroEnergy = 0;
                chosenCountry.oilEnergy = 0;
                chosenCountry.gasEnergy = 1;
                chosenCountry.nuclearEnergy = 0;
            }
            else if (emissionData.energyProduction == EnergyProduction.RENEWABLE_WITH_EMISSIONS) {
                chosenCountry.coalEnergy = 0;
                chosenCountry.hydroEnergy = 1;
                chosenCountry.oilEnergy = 0;
                chosenCountry.gasEnergy = 0;
                chosenCountry.nuclearEnergy = 0;
            }
            else if (emissionData.energyProduction == EnergyProduction.NUCLEAR_RENEWABLE) {
                chosenCountry.coalEnergy = 0;
                chosenCountry.hydroEnergy = 0;
                chosenCountry.oilEnergy = 0;
                chosenCountry.gasEnergy = 0;
                chosenCountry.nuclearEnergy = 1;
            }
        } catch(e:Error) {
            Alert.show("emissionUpdate error: " + e.message);
        }
    }


    public function changeViewState(bookmark:int):void {
        switch (bookmark) {
            case 10:
                mainApp.currentState = FossilStateNames.FIRST_INITIALIZATION;
                break;
            case 1:
                mainApp.currentState = FossilStateNames.CHOOSE_COUNTRY;
                break;
            case 2:
                mainApp..currentState = FossilStateNames.HOUSE;
                contentView.changeView(1);
                break;
            case 3:
                mainApp..currentState = FossilStateNames.WARM_WATER;
                contentView.changeView(2);
                break;
            case 4:
                mainApp..currentState = FossilStateNames.PRIVATE_TRANSPORT;
                contentView.changeView(3);
                break;
            case 5:
                mainApp..currentState = FossilStateNames.PUBLIC_TRANSPORT;
                contentView.changeView(4);
                break;
            case 6:
                mainApp..currentState = FossilStateNames.FOOD;
                contentView.changeView(5);
                break;
            case 7:
                mainApp..currentState = FossilStateNames.CONSUMPTION;
                contentView.changeView(6);
                break;
            case 8:
                mainApp..currentState = FossilStateNames.EMISSIONS;
                contentView.changeView(7);

                if (newEmissions == null) {
                    newEmissions = oldEmissions.copy();
                }

                try {
                    this.doCalculation(mainApp.wykres1.getSelectedUnit());
                    this.setPlotValues(true);
                } catch(e:Error) {
                    Alert.show("Calculate click error:" + e.message);
                }



                break;
        }
    }

    private function initializeSummary():void {
        var oldEmissionConverted:ConvertedEmission = EmissionHelper.convertToUnits(oldEmissions, -1);
        var emissionOld:Number = oldEmissionConverted.overallEmission;
        var emissionNew:Number = newEmissions != null ? EmissionHelper.convertToUnits(newEmissions, -1).overallEmission : emissionOld;
        var emissionAvg:Number = oldEmissionConverted.averageEmission;

        setEmissionTextValues("PL", emissionOld, emissionNew, emissionAvg);
    }






    public function setInfoTexts():void {
        var currentEmission:CountryEmission = newEmissions != null ? newEmissions : oldEmissions;

        var treeNumber:Number = EmissionHelper.getNumberOfTreesEquivalent(currentEmission);
        var peopleNumber:Number = EmissionHelper.getNumberOfPeopleEquivalent(currentEmission);
        var emissionRatio:Number = EmissionHelper.getAverageEmissionRatio(currentEmission);

        var format:NumberFormatter = new NumberFormatter();
        format.precision = 0;
        format.rounding = NumberBaseRoundType.NEAREST;

        setTreeNumberText(format.format(treeNumber));
        setPeopleNumberText(format.format(peopleNumber));
        emissionCO2Texts(format.format(emissionRatio));
        setReserveValueText();
    }

    public function setPeopleNumberText(peopleNumberText:String):void {
        infoControls.setEnergyInfo(peopleNumberText);
    }

    public function setTreeNumberText(treeNumberText:String):void {
        infoControls.setTreesInfo(treeNumberText);
    }

    public function setReserveValueText():void {
        var format:NumberFormatter = new NumberFormatter();
        format.precision = 0;
        format.rounding = NumberBaseRoundType.NEAREST;

        var oilReserve:Number = new Number(Number(mainPlot.lblZuzycieRopa.text.replace(",", ".")));
        var coalReserve:Number = new Number(Number(mainPlot.lblZuzycieWegiel.text.replace(",", ".")));
        var gasReserve:Number = new Number(Number(mainPlot.lblZuzycieGaz.text.replace(",", ".")));

        oilReserve = 188668480000000 / (oilReserve * 7000000000);
        coalReserve = 860938000000000 / (coalReserve * 7000000000);
        gasReserve = 149680000000000 / (gasReserve * 7000000000);

        //Okropnie chujowo zrobione do poprawy :p
        infoControls.setFossilFuelsInfo(format.format(oilReserve),
                format.format(coalReserve), format.format(gasReserve));
    }


    public function emissionCO2Texts(emissionValue:String):void {
        infoControls.setEmissionText(emissionValue);
        //lblEmisje3.text = format.format(emisje);
    }

    private function setEmissionTextValues(lang:String, emissionOld:Number, emissionNew:Number, emissionAvg:Number):void {
        var avgEmissionRatio:Number = new Number(0);

        try {

            avgEmissionRatio = emissionNew / emissionAvg;

            aboveAverageCO2Label.visible = false;
            textArea.visible = false;
            textArea1.visible = false;
            textArea2.visible = false;

        } catch(e:Error) {
            Alert.show("set text error: " + e.message);
        }

        if (emissionOld == emissionNew) {
            aboveAverageCO2Label.visible = true;
            textArea.visible = true;

            try {
                if (emissionOld < 0.5 * emissionAvg) {
                    aboveAverageCO2Label.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 0).(zakres_gora == 0.5).tresc_tytul;
                    textArea.htmlText = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 0).(zakres_gora == 0.5).tresc_dol;
                }
            } catch(e:Error) {
                Alert.show("emissionOld < 0.5 * emissionAvg " + e.message);
            }

            try {
                if (emissionOld >= 0.5 * emissionAvg && emissionOld < 0.8 * emissionAvg) {
                    aboveAverageCO2Label.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 0.5).(zakres_gora == 0.8).tresc_tytul;
                    textArea.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 0.5).(zakres_gora == 0.8).tresc_dol;
                }
            } catch(e:Error) {
                Alert.show("emissionOld (0.5;0,8) * emissionAvg " + e.message);
            }


            try {
                if (emissionOld >= 0.8 * emissionAvg && emissionOld < 1.2 * emissionAvg) {
                    aboveAverageCO2Label.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 0.8).(zakres_gora == 1.2).tresc_tytul;
                    textArea.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 0.8).(zakres_gora == 1.2).tresc_dol;
                }
            } catch(e:Error) {
                Alert.show("emissionOld (0.8;1,2) * emissionAvg " + e.message);
            }


            try {
                if (emissionOld >= 1.2 * emissionAvg && emissionOld < 1.8 * emissionAvg) {
                    aboveAverageCO2Label.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 1.2).(zakres_gora == 1.8).tresc_tytul;
                    textArea.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 1.2).(zakres_gora == 1.8).tresc_dol;
                }
            } catch(e:Error) {
                Alert.show("emissionOld (1.2;1,8) * emissionAvg " + e.message);
            }


            try {
                if (emissionOld >= 1.8 * emissionAvg && emissionOld < 2.5 * emissionAvg) {
                    aboveAverageCO2Label.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 1.8).(zakres_gora == 2.5).tresc_tytul;
                    textArea.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 1.8).(zakres_gora == 2.5).tresc_dol;
                }
            } catch(e:Error) {
                Alert.show("emissionOld (1.8;2,5) * emissionAvg " + e.message);
            }


            try {
                if (emissionOld >= 2.5 * emissionAvg && emissionOld < 10 * emissionAvg) {
                    aboveAverageCO2Label.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 2.5).(zakres_gora == 10).tresc_tytul;
                    textArea.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 2.5).(zakres_gora == 10).tresc_dol;
                }
            } catch(e:Error) {
                Alert.show("emissionOld (2,5;10 * emissionAvg " + e.message);
            }


            try {
                if (emissionOld > 10 * emissionAvg) {
                    aboveAverageCO2Label.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 10).(zakres_gora == 1000000).tresc_tytul;
                    textArea.text = emissionTexts.przejscie.(numer == 1).tekst.(zakres_dol == 10).(zakres_gora == 1000000).tresc_dol;
                }
            } catch(e:Error) {
                Alert.show("emissionOld > 10 * emissionAvg " + e.message);
            }
        }
        else if (emissionOld < emissionNew) {
            try {
                aboveAverageCO2Label.visible = true;
                textArea.visible = true;
                textArea1.visible = false;
                textArea2.visible = true;

                aboveAverageCO2Label.text = emissionTexts.przejscie.(numer == 2).text_neg;
                textArea.text = emissionTexts.przejscie.(numer == 2).text_glowny2;
                textArea2.text = emissionTexts.przejscie.(numer == 2).text_koniec;
            } catch(e:Error) {
                Alert.show("Emission old < emissionNew " + e.message);
            }

        }
        else if (emissionOld > emissionNew) {
            var oszczednosc:Number = new Number(0);
            var procent_oszczednosci:String = new String("");
            oszczednosc = ((emissionOld - emissionNew) / emissionOld) * 100;
            var format:NumberFormatter = new NumberFormatter();
            format.precision = 2;
            format.rounding = NumberBaseRoundType.NEAREST;

            procent_oszczednosci = format.format(oszczednosc);

            try {
                aboveAverageCO2Label.visible = true;
                textArea.visible = true;
                textArea1.visible = true;
                textArea2.visible = true;

                aboveAverageCO2Label.text = emissionTexts.przejscie.(numer == 2).text_glowny1 + " " + procent_oszczednosci + "%";
                //txtTekstArea.text = tekstyEmisjaXML.przejscie.(numer==2).text_glowny2;
                textArea1.text = emissionTexts.przejscie.(numer == 2).text_glowny2;
                textArea2.text = emissionTexts.przejscie.(numer == 2).text_koniec;
            } catch(e:Error) {
                Alert.show("Emission old > emissionNew set text error: " + e.message);
            }

            try {
                if (emissionOld < 0.8 * emissionAvg) {
                    if (oszczednosc > 0 && oszczednosc <= 10) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0).(zakres_gora == 0.8).ograniczenie.(od == 0).(do1 == 10).tresc;
                    }
                    else if (oszczednosc > 10 && oszczednosc <= 20) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0).(zakres_gora == 0.8).ograniczenie.(od == 10).(do1 == 20).tresc;
                    }
                    else if (oszczednosc > 20 && oszczednosc <= 50) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0).(zakres_gora == 0.8).ograniczenie.(od == 20).(do1 == 50).tresc;
                    }
                    else if (oszczednosc > 50 && oszczednosc <= 100) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0).(zakres_gora == 0.8).ograniczenie.(od == 50).(do1 == 100).tresc;
                    }
                }
            } catch(e:Error) {
                Alert.show("Error emission < 0,8 " + e.message);
            }


            try {
                if (emissionOld >= 0.8 * emissionAvg && emissionOld < 1.2 * emissionAvg) {
                    if (oszczednosc > 0 && oszczednosc <= 10) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0.8).(zakres_gora == 1.2).ograniczenie.(od == 0).(do1 == 10).tresc;
                    }
                    else if (oszczednosc > 10 && oszczednosc <= 20) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0.8).(zakres_gora == 1.2).ograniczenie.(od == 10).(do1 == 20).tresc;
                    }
                    else if (oszczednosc > 20 && oszczednosc <= 50) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0.8).(zakres_gora == 1.2).ograniczenie.(od == 20).(do1 == 50).tresc;
                    }
                    else if (oszczednosc > 50 && oszczednosc <= 100) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0.8).(zakres_gora == 1.2).ograniczenie.(od == 50).(do1 == 100).tresc;
                    }
                }
            } catch(e:Error) {
                Alert.show("Error emission (0,8 - 1.2) " + e.message);
            }


            try {
                if (emissionOld >= 1.2 * emissionAvg && emissionOld < 1.8 * emissionAvg) {
                    if (oszczednosc > 0 && oszczednosc <= 10) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0.8).(zakres_gora == 1.2).ograniczenie.(od == 0).(do1 == 10).tresc;
                    }
                    else if (oszczednosc > 10 && oszczednosc <= 20) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0.8).(zakres_gora == 1.2).ograniczenie.(od == 10).(do1 == 20).tresc;
                    }
                    else if (oszczednosc > 20 && oszczednosc <= 50) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0.8).(zakres_gora == 1.2).ograniczenie.(od == 20).(do1 == 50).tresc;
                    }
                    else if (oszczednosc > 50 && oszczednosc <= 100) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 0.8).(zakres_gora == 1.2).ograniczenie.(od == 50).(do1 == 100).tresc;
                    }
                }
            } catch(e:Error) {
                Alert.show("Error emission (1,2 - 1.8) " + e.message);
            }

            try {
                if (emissionOld >= 1.8 * emissionAvg && emissionOld < 2.5 * emissionAvg) {
                    if (oszczednosc > 0 && oszczednosc <= 10) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 1.2).(zakres_gora == 2.5).ograniczenie.(od == 0).(do1 == 10).tresc;
                    }
                    else if (oszczednosc > 10 && oszczednosc <= 20) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 1.2).(zakres_gora == 2.5).ograniczenie.(od == 10).(do1 == 20).tresc;
                    }
                    else if (oszczednosc > 20 && oszczednosc <= 50) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 1.2).(zakres_gora == 2.5).ograniczenie.(od == 20).(do1 == 50).tresc;
                    }
                    else if (oszczednosc > 50 && oszczednosc <= 100) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 1.2).(zakres_gora == 2.5).ograniczenie.(od == 50).(do1 == 100).tresc;
                    }
                }
            } catch(e:Error) {
                Alert.show("Error emission (1,8- 2.5) " + e.message);
            }

            try {
                if (emissionOld >= 2.5 * emissionAvg && emissionOld < 10 * emissionAvg) {
                    if (oszczednosc > 0 && oszczednosc <= 10) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 2.5).(zakres_gora == 10).ograniczenie.(od == 0).(do1 == 10).tresc;
                    }
                    else if (oszczednosc > 10 && oszczednosc <= 20) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 2.5).(zakres_gora == 10).ograniczenie.(od == 10).(do1 == 20).tresc;
                    }
                    else if (oszczednosc > 20 && oszczednosc <= 50) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 2.5).(zakres_gora == 10).ograniczenie.(od == 20).(do1 == 50).tresc;
                    }
                    else if (oszczednosc > 50 && oszczednosc <= 100) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 2.5).(zakres_gora == 10).ograniczenie.(od == 50).(do1 == 100).tresc;
                    }
                }
            } catch(e:Error) {
                Alert.show("Error emission (2,5 - 10) " + e.message);
            }

            try {
                if (emissionOld > 10 * emissionAvg) {
                    if (oszczednosc > 0 && oszczednosc <= 10) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 10).(zakres_gora == 100000).ograniczenie.(od == 0).(do1 == 10).tresc;
                    }
                    else if (oszczednosc > 10 && oszczednosc <= 20) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 10).(zakres_gora == 100000).ograniczenie.(od == 10).(do1 == 20).tresc;
                    }
                    else if (oszczednosc > 20 && oszczednosc <= 50) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 10).(zakres_gora == 100000).ograniczenie.(od == 20).(do1 == 50).tresc;
                    }
                    else if (oszczednosc > 50 && oszczednosc <= 100) {
                        textArea.text = emissionTexts.przejscie.(numer == 2).tekst.(zakres_dol == 10).(zakres_gora == 100000).ograniczenie.(od == 50).(do1 == 100).tresc;
                    }
                }
            } catch(e:Error) {
                Alert.show("Error emission > 10 " + e.message);
            }
        }


    }

    public function setCalcHelpState(value:MainCalculator):void {
        mainCalculator = value;
    }

    public function setControlButtonStates(value:ButtonStatesController):void {
        controlButtonStates = value;
    }

    public function setCalcCode(value:CoefficientCalculator):void {
        coefficientCalculator = value;
    }

    public function setMainPlot(value:PlotPanel):void {
        mainPlot = value;
    }

    public function setCountryXML(value:XML):void {
        countryXML = value;
    }

    public function setAboveAverageCO2Label(value:Label):void {
        aboveAverageCO2Label = value;
    }

    public function setEmmissionTexts(value:XML):void {
        emissionTexts = value;
    }

    public function setTextAreas(value1:TextArea, value2:TextArea, value3:TextArea):void {
        this.textArea = value1;
        this.textArea1 = value2;
        this.textArea2 = value3;
    }

    public function setCountrySelector(value:ChoseCountryPanel):void {
        countrySelector = value;
    }

    public function setNewToolTip(value:ToolTipPanel):void {
        newToolTip = value;
    }

    public function setSceneView(value:ViewContentPanel):void {
        contentView = value;
    }

    public function setTopPart(value:TopPanel):void {
        topPart = value;
    }

    public function setMainMenu(value:MenuControlsPanel):void {
        mainMenu = value;
    }

    public function setInfoControls(value:InfoPanel):void {
        infoControls = value;
    }

    public function setCanvas1(canvas:Canvas) {
        canvas1 = canvas;
    }

}
}



	
